<!DOCTYPE html>
<html>
  
  <head>
    <meta charset="UTF-8">
    <title>车源添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="__STATIC__/admin/static/css/font.css">
		<link rel="stylesheet" href="__STATIC__/admin/static/css/weadmin.css">
    <script type="text/javascript" src='__STATIC__/admin/static/js/jquery3.2.1.min.js'></script>
    <style>    
      #X{
        position: relative;
        top: -40px;
        left:-18px;
        color:red;
        font-size: 25px;
        background: white;
        border-radius: 30px;
      }

    </style>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  
  <body>
    <div class="weadmin-body">
        <form class="layui-form">
          
          <div class="layui-form-item">
              <label for="aid" class="layui-form-label">
                  <span class="we-red">*</span>地区
              </label>
              <div class="layui-input-inline" style="width:190px;">
                <select name="pid" lay-verify="" lay-filter='pid'>
                  {foreach $area as $v}
                    <option value="{$v['id']}">{$v['first']}  {$v['name']}</option>
                  {/foreach}
                </select>
              </div>
              <div class="layui-input-inline" style="width:190px;">
                <select name="aid" lay-verify="" lay-filter='aid'>
                  {foreach $area2 as $v}
                    <option value="{$v['id']}">{$v['first']}  {$v['name']}</option>
                  {/foreach}
                </select>     
              </div>
              <div class="layui-form-mid layui-word-aux">
                  <span class="we-red">*</span>
              </div>
          </div>
          <div class="layui-form-item">
              <label for="bid" class="layui-form-label">
                  <span class="we-red">*</span>品牌
              </label>
              <div class="layui-input-inline" style="width:190px;">
                <select name="bid" lay-verify="" lay-filter='bid'>
                  {foreach $brand as $v}
                    <option value="{$v['bid']}">{$v['first']}  {$v['bname']}</option>
                  {/foreach}
                </select>
              </div>
              <div class="layui-input-inline" style="width:190px;">
                <select name="cid" lay-verify="" lay-filter='cid'>
                  
                </select>     
              </div>
              <div class="layui-input-inline" style="width:190px;">
                <select name="tid" lay-verify="" lay-filter='tid'>
                  
                </select>     
              </div>
              <div class="layui-form-mid layui-word-aux">
                  <span class="we-red">*</span>
              </div>
          </div>
          <div class="layui-form-item">
              <label for="licensetime" class="layui-form-label">
                  <span class="we-red">*</span>上牌日期
              </label>
              <div class="layui-input-inline">
                 <input class="layui-input" name="licensetime" id="time" required="" lay-verify="required" >
              </div>
              <div class="layui-form-mid layui-word-aux">
                  <span class="we-red">*</span>
              </div>
          </div>
          <div class="layui-form-item">
              <label for="distance" class="layui-form-label">
                  <span class="we-red">*</span>行驶里程
              </label>
              <div class="layui-input-inline">
                 <input class="layui-input" name="distance" id="distance" required="" lay-verify="required" >
              </div>
              <div class="layui-form-mid layui-word-aux">
                  <span>万公里</span>
              </div>
              <div class="layui-form-mid layui-word-aux">
                  <span class="we-red">*</span>
              </div>
          </div>
          <div class="layui-form-item">
              <label for="insurance" class="layui-form-label">
                  <span class="we-red">*</span>交强险日期
              </label>
              <div class="layui-input-inline">
                 <input class="layui-input" name="insurance" id="time" required="" lay-verify="required" >
              </div>
              <div class="layui-form-mid layui-word-aux">
                  <span class="we-red">*</span>
              </div>
          </div>
          <div class="layui-form-item">
              <label for="cktime" class="layui-form-label">
                  <span class="we-red">*</span>年检截至日
              </label>
              <div class="layui-input-inline">
                 <input class="layui-input" name="cktime" id="time" required="" lay-verify="required" >
              </div>
              <div class="layui-form-mid layui-word-aux">
                  <span class="we-red">*</span>
              </div>
          </div>
          <div class="layui-form-item">
              <label for="preprice" class="layui-form-label">
                  <span class="we-red">*</span>预期价格
              </label>
              <div class="layui-input-inline">
                 <input class="layui-input" name="preprice" id="preprice" required="" lay-verify="required" >
              </div>
              <div class="layui-form-mid layui-word-aux">
                  <span>万</span>
              </div>
              <div class="layui-form-mid layui-word-aux">
                  <span class="we-red">*</span>
              </div>
          </div>
          {foreach $config as $v}
            {if $v['type'] == 'time'}
              <div class="layui-form-item">
                  <label for="cktime" class="layui-form-label">
                      <span class="we-red">*</span>{$v['name']}
                  </label>
                  <div class="layui-input-inline">
                     <input class="layui-input" name="config[{$v['cid']}]" id="time">
                  </div>
              </div>
            {elseif $v['type'] == 'select' /}
              <div class="layui-form-item">
                  <label for="{$v['name']}" class="layui-form-label">
                      <span class="we-red">*</span>{$v['name']}
                  </label>
                  <div class="layui-input-inline">
                    <select name="config[{$v['cid']}]" lay-verify="">
                      {volist name=":explode(',',$v['val'])" id="v1"}
                      <option value="{$v1}">{$v1}</option>
                      {/volist}
                    </select>     
                  </div>
              </div> 
            {elseif $v['type'] == 'checkbox' /}
              <div class="layui-form-item">
                  <label for="{$v['name']}" class="layui-form-label">
                      <span class="we-red">*</span>{$v['name']}
                  </label>
                  <div class="layui-input-inline">
                      {volist name=":explode(',',$v['val'])" id="v1"}
                      <input type="checkbox" name="config[{$v['cid']}][]" title="{$v1}" lay-skin="primary" value="{$v1}">
                      {/volist}      
                  </div>
              </div> 
            {else/}
              <div class="layui-form-item">
              <label for="{$v['sign']}" class="layui-form-label">
                  <span class="we-red">*</span>{$v['name']}
              </label>
              <div class="layui-input-inline">
                 <input class="layui-input" name="config[{$v['cid']}]" id="{$v['sign']}">
              </div>
              <div class="layui-form-mid layui-word-aux">
                  <span class="we-red">*</span>
              </div>
          </div>
            {/if}
          {/foreach}
          <div class="layui-form-item">
              <label for="logo" class="layui-form-label">
                  汽车图片
              </label>
              <div class="layui-input-inline">
                  <div class="hidden"></div>
                  <button type="button" class="layui-btn" id="logo">
                    <i class="layui-icon">&#xe67c;</i>上传图片
                  </button>
                  
              </div>
          </div>
          <div class="layui-input-inline" id="demo" style="margin-left: 100px"></div>
          <div class="layui-form-item"></div>
          <div class="layui-form-item">
              <label for="oname" class="layui-form-label">
                  <span class="we-red"></span>车主姓名
              </label>
              <div class="layui-input-inline">
                  <input type="text" id="oname" name="oname" required="" lay-verify="required" autocomplete="off" class="layui-input">
              </div>
          </div>
          <div class="layui-form-item">
              <label for="ophone" class="layui-form-label">
                  <span class="we-red"></span>联系电话
              </label>
              <div class="layui-input-inline">
                  <input type="text" id="ophone" name="ophone" required="" lay-verify="required|phone" autocomplete="off" class="layui-input">
              </div>
          </div>
          <div class="layui-form-item">
              <label for="desc" class="layui-form-label">
                  <span class="we-red"></span>补充描述
              </label>
              <div class="layui-input-inline">
                  <textarea name="desc" id="desc" cols="25" rows="10"></textarea>
              </div>
          </div>
          <div class="layui-form-item">
              <label for="L_repass" class="layui-form-label"></label>
              <button  class="layui-btn" lay-filter="add" lay-submit="" id="add">增加</button>
          </div>
      </form>
    </div>
		<script src="__STATIC__/admin/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript">
    		layui.extend({
					admin: '{/}__STATIC__/admin/static/js/admin'
				});
        layui.use(['form','layer','admin','upload','laydate'], function(){
          var form = layui.form,
          	admin = layui.admin,
          	layer = layui.layer;
            upload = layui.upload;
            laydate = layui.laydate;
          	form.render();
            laydate.render({ 
              elem: '#time'
            });
          //监听提交
          form.on('submit(add)', function(data){
            obj = $('#demo').find('img');
            if(obj.length == 0){
              layer.msg('请上传图片',{icon:2,time:1000},function(){
              });
            }
            return false;
          });
         //地区联动
         form.on('select(pid)', function(data){
            $.ajax({
                type: 'GET',
                url: "{:url('admin/carsource/carsourceadd')}",
                data:"pid="+data.value,
                success: function(data){
                  str = '';
                  for(i in data){
                    str+="<option value='"+data[i].id+"'>"+data[i].first+" "+data[i].name+"</option>"
                  }
                  $("select[name='aid']").html(str);
                  form.render();
                }
            });
          });
         //品牌联动
         form.on('select(bid)', function(data){
            $.ajax({
                type: 'GET',
                url: "{:url('admin/carsource/carsourceadd')}",
                data:"bid="+data.value,
                success: function(data){
                  str = '';
                  for(i in data.gname){
                    str+="<option value='0' disabled>----"+data.gname[i].gname+"----</option>";
                    for(k in data.cate){
                      if(data.cate[k].gname == data.gname[i].gname){
                        str+="<option value='"+data.cate[k].id+"'>"+data.cate[k].name+"</option>";
                      }
                    }
                  }
                  $("select[name='cid']").html(str);
                  ssr = '';
                  for(a in data.type){
                    ssr+="<option value='"+data.type[a].tid+"'>"+data.type[a].tname+"</option>";
                  }
                  $("select[name='tid']").html(ssr);
                  form.render();
                }
            });
          });
          form.on('select(cid)', function(data){
            $.ajax({
                type: 'GET',
                url: "{:url('admin/carsource/carsourceadd')}",
                data:"cid="+data.value,
                success: function(data){
                  str = '';
                  for(i in data){
                    str+="<option value='"+data[i]['tid']+"'>"+data[i]['tname']+"</option>";
                  }
                  $("select[name='tid']").html(str);
                  form.render();
                }
            });
          });
          //执行实例
            var uploadInst = upload.render({
              elem: '#logo' //绑定元素
              ,url: "{:url('admin/carsource/carsourceadd')}" //上传接口
              ,field:"image"
              ,multiple: true
              ,accept: "images"
              ,bindAction: '#add'
              ,auto:false
              ,data:{
                img: function(){
                  return $("input[name='img']").val();
                }
              }
              ,choose:function(obj){
                var files = this.files = obj.pushFile();
                obj.preview(function(index, file, result){
                  if((file.type).indexOf("image") >= 0 ){
                    $('#demo').append('<span><img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img" style="height:100px"></span>')
                  }
                  $("#demo").find("img").on('click',function(){
                    delete files[index]; //删除对应的文件
                    $(this).remove();
                  })
                });
              }
              ,done: function(res, index, upload){

                $(".hidden").append("<input type='hidden' name='img[]' value='"+res.im+"'/>");
                delete this.files[index];
                $("#demo").find("img").remove();

              }
              ,allDone: function(obj){ //当文件全部被提交后，才触发
                $.ajax({
                  type: 'POST',
                  url: "{:url('admin/carsource/carsourceadd')}",
                  data:$('form').serialize(),
                  traditional:true,
                  success: function(data){
                      if(data.code==1){
                        layer.msg(data.msg,{icon:1,time:1000},function(){
                            parent.location.reload();
                            parent.layer.close();
                        });
                      }else{
                        layer.msg(data.msg,{icon:2,time:1000},function(){

                        });
                      }
                  }
                });
                $(".hidden").find("input").remove();
              }
              ,error: function(){
                //请求异常回调
              }
            });
        });
    </script>
  </body>

</html>


